home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
ada
/
gwuada_9.zip
/
PREDEF4.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-27
|
24KB
|
895 lines
/*
* Copyright (C) 1985-1992 New York University
*
* This file is part of the Ada/Ed-C system. See the Ada/Ed README file for
* warranty (none) and distribution info and also the GNU General Public
* License for more details.
*/
/* +---------------------------------------------------+
| |
| I N T E R P P R E D E F S |
| Part 4: Input/Output Procedures |
| (C Version) |
| |
| Adapted From Low Level SETL version written by |
| |
| Monte Zweben |
| Philippe Kruchten |
| Jean-Pierre Rosen |
| |
| Original High Level SETL version written by |
| |
| Clint Goss |
| Tracey M. Siesser |
| Bernard D. Banner |
| Stephen C. Bryant |
| Gerry Fisher |
| |
| C version written by |
| |
| Robert B. K. Dewar |
| |
+---------------------------------------------------+
*/
/* This module contains routines for the implementation of some of
* the predefined Ada packages and routines, namely SEQUENTIAL_IO,
* DIRECT_IO, TEXT_IO, and CALENDAR. Part 4 contains the initialization
* and termination routines for predef, and the basic I/O routines
*/
#include <stdlib.h>
#include <string.h>
#ifdef IBM_PC
#include <io.h>
#endif
#include "ipredef.h"
#include "miscp.h"
#include "predefp.h"
static void check_ifile_closed(int *);
static void check_xfile_closed(char *);
static void open_file();
#ifdef IBM_PC
#undef putc
#define putc(A, B) fputc((A),(B));fflush(B)
#endif
#ifdef GWUMON
#undef putc
#define putc(A, B) CWK_PUTC((A),(B))
#endif
#ifdef DEBUG_PREDEF
static void gchar(char *, int);
static void pchar(char *, int);
#endif
/* AFCB for STANDARD_IN_FILE */
static struct afcb in_afcb = {
0, /* file descriptor for standard input */
"", /* file name(null) */
"", /* form string(null) */
TIO_IN_FILE, /* mode, TEXT_IO input */
0, 0, 0, /* unused DIRECT_IO fields */
1, /* page number */
1, /* line number */
1, /* column number */
0, /* unbounded line length */
0, /* unbounded page length */
0, " " /* look ahead */
};
#ifdef IBM_PC
/* keep track of last character read from stdin so can detect whether we
* need to flush what is left there before exiting
*/
static int last_char_input = EOF;
#endif
/* AFCB for STANDARD_OUT_FILE */
static struct afcb out_afcb = {
0, /* file descriptor for standard input */
"", /* file name(null) */
"", /* form string(null) */
TIO_OUT_FILE, /* mode, TEXT_IO output */
0, 0, 0, /* unused DIRECT_IO fields */
1, /* page number */
1, /* line number */
1, /* column number */
0, /* unbounded line length */
0, /* unbounded page length */
0, " " /* look ahead */
};
/* Procedure to initialize input/output data structures */
void initialize_predef() /*;initialize_predef*/
{
/* Clear temporary file list, and clear AFCB vector */
tfiles = 0;
for (filenum = 1; filenum <= MAXFILES; filenum++)
afcbs[filenum - 1] = 0;
/* Setup references for current and standard files */
current_in_file = 1;
current_in_file_saved = 1;
standard_in_file = 1;
afcbs[0] = &in_afcb;
filenum = 1;
IOFDESC = stdin;
CHARS = 0;
current_out_file = 2;
current_out_file_saved = 2;
standard_out_file = 2;
afcbs[1] = &out_afcb;
filenum = 2;
IOFDESC = stdout;
/* Set standard exception signalled on bad data (changed temporarily
* to CONSTRAINT_ERROR when TEXT_IO routines are called directly from
* the main interpretor for the IMAGE attribute.
*/
data_exception = DATA_ERROR;
}
/* CHECK_OPENED_OK */
/* Checks that an fopen succeeded, raise USE_ERROR if not */
void check_opened_ok() /*;check_opened_ok*/
{
if (IOFDESC == NULL)
predef_raise(USE_ERROR, "Error opening or resetting file");
}
/* CHECK_IFILE_CLOSED */
/* Checks that the file object stored at file_ptr is closed */
static void check_ifile_closed(int *file_ptr) /*;check_ifile_closed*/
{
int file_val;
file_val = *file_ptr;
if (file_val != 0)
predef_raise(STATUS_ERROR, "File not closed");
}
/* CHECK_XFILE_CLOSED */
/* Checks that no external file with a matching name is currently open */
static void check_xfile_closed(char *fname) /*;check_xfile_closed*/
{
int i;
for (i = 1; i <= MAXFILES; i++) {
if (afcbs[i - 1] == NULL) continue;
if (strcmp(fname, afcbs[i - 1] -> io_fname) == 0 &&
afcbs[i - 1] -> io_fdesc != NULL) {
predef_raise(USE_ERROR, "File already open");
}
}
}
/* CHECK_FILE_OPEN */
/* Check if the current file is open or not. If the file is not open,
* then STATUS_ERROR is raised. Otherwise control returns normally.
*/
void check_file_open() /*;check_file_open*/
{
if (filenum == 0)
predef_raise(STATUS_ERROR, "File not open");
}
/* If the current file is not open, then STATUS_ERROR is raised. If
* the file is open, then the mode is checked against the argument which
* is the desired mode for the operation. If it does not match, then
* MODE_ERROR is raised, otherwise control returns normally.
*/
void check_status(int c_mode) /*;check_status*/
{
check_file_open();
if (IOMODE != c_mode)
predef_raise(MODE_ERROR, "Incorrect file status");
}
/* Routine called by the OPEN and CREATE portions of the PREDEF procedure
* to perform common data structure operations for TEXT_IO operations.
* The operation in opn is 'C' for a create and 'O' for an open.
*/
void open_textio(char opn) /*;open_textio*/
{
open_file();
#ifdef SYSTEM_V
if (strlen(IOFNAME) > 14) predef_raise(NAME_ERROR,"Invalid file name");
#endif
if (opn == 'C') {
IOFDESC = fopen_txt(IOFNAME, "w");
if (IOFDESC == NULL) predef_raise(NAME_ERROR,"Invalid file name");
if (IOMODE == SIO_IN_FILE) {
fclose(IOFDESC);
IOFDESC = fopen_txt(IOFNAME, "r");
check_opened_ok();
}
}
else { /* opn == 'O' */
/*
* According to AI-00048:
* Opening a file with IN_FILE mode which is the default output file
* will raise MODE_ERROR.
* Opening a file with OUT_FILE mode which is the default input file
* will raise MODE_ERROR.
* The values to be checked is in current_in_file_saved and
* current_out_file_saved which are copies of the file numbers
* associated with the default files. These copies must be used
* because when the default files are closed their filenums saved
* in current_XXX_file are set to zero and therefore lost for this
* check.
*/
if (filenum == current_in_file_saved && IOMODE == TIO_OUT_FILE)
predef_raise(MODE_ERROR,"File is default in file");
i